library(readstata13)
library(stargazer)
library(gmediation)

setwd("C:/Users/kms13/Dropbox/DATA AND CODEBOOK/R-R")


#FIGURE 4: MAIN MEDIATION MODEL
dat <- read.dta13("LSQ_replication.dta")
dat <- na.omit(dat)

model.y2.6 <- glm(approv ~ sex + prefmed + comDipPresi + comm_assign + ncomdip + mbloq + nComDipMember + tenure + lnfirm + copartisan_in_executive + divgov 
                  + natquota + maj + totalbills, data = dat, family = binomial)

model.m11.6 <- glm(comDipPresi ~ sex + nComDipMember + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)

model.m12.6 <- glm(comm_assign ~ sex + nComDipMember + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)

model.m21.6 <- glm(prefmed ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)



mediate7 <- gmediate(data = dat, model.m1 = list(model.m11.6, model.m12.6), model.m2 = list(model.m21.6), model.y = model.y2.6, expos = c("sex", 1), cluster = "id_rep")

summary(mediate7)
mediate7$indiv.path
mediate7$second.path

summary(model.m11.6)
summary(model.m12.6)
summary(model.m21.6)
summary(model.y2.6)

confint(model.y2.6)
confint(model.m11.6)
confint(model.m12.6)
confint(model.m21.6)

stargazer(model.y2.6, model.m11.6, model.m12.6, model.m21.6, type = "latex", out = "mediation_models_R&R2.tex")


#APPENDIX FIGURE A1: WOMENS BILLS + # REFERALS + MULTIPARTY 
dat <- read.dta13("LSQ_replication.dta")
dat <- na.omit(dat)

model.y2.10 <- glm(approv ~ sex + prefmed + comDipPresi + comm_assign + ncomdip + mbloq + nComDipMember + tenure + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)

model.m11.10 <- glm(comDipPresi ~ sex + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m12.10 <- glm(comm_assign ~ sex + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m21.10 <- glm(prefmed ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m22.10 <- glm(mbloq ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m23.10 <- glm(ncomdip ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat)


mediate11 <- gmediate(data = dat, model.m1 = list(model.m11.10, model.m12.10), model.m2 = list(model.m21.10, model.m22.10, model.m23.10), model.y = model.y2.10, expos = c("sex", 1), cluster = "id_rep")


mediate11$indiv.path
mediate11$second.path

summary(model.m11.10)
summary(model.m12.10)
summary(model.m21.10)
summary(model.y2.10)

confint(model.y2.10)
confint(model.m11.10)
confint(model.m12.10)
confint(model.m21.10)


#APPENDIX FIGURE A2: WOMENS BILLS + TOTAL BILLS + # COSPONSORS
dat <- read.dta13("LSQ_replication.dta")
dat <- na.omit(dat)

model.y2.11 <- glm(approv ~ sex + prefmed + comDipPresi + comm_assign + nComDipMember + tenure + lnfirm + copartisan_in_executive + divgov 
                   + natquota + maj + totalbills, data = dat, family = binomial)

model.m11.11 <- glm(comDipPresi ~ sex + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m12.11 <- glm(comm_assign ~ sex + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m21.11 <- glm(prefmed ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m22.11 <- glm(totalbills ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat)

model.m23.11 <- glm(lnfirm ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat)


mediate12 <- gmediate(data = dat, model.m1 = list(model.m11.11, model.m12.11), model.m2 = list(model.m21.11, model.m22.11, model.m23.11), model.y = model.y2.11, expos = c("sex", 1), cluster = "id_rep")

mediate12$indiv.path
mediate12$second.path
summary(model.m11.11)
summary(model.m12.11)
summary(model.m21.11)
summary(model.y2.11)

confint(model.y2.11)
confint(model.m11.11)
confint(model.m12.11)
confint(model.m21.11)


#APENDIX FIGURE A3: REMOVE ALL POST-TREATMENT
dat <- read.dta13("LSQ_replication.dta")
dat <- na.omit(dat)

model.y2.12 <- glm(approv ~ sex + prefmed + comDipPresi + comm_assign + nComDipMember + tenure + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)

model.m11.12 <- glm(comDipPresi ~ sex + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m12.12 <- glm(comm_assign ~ sex + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)

model.m21.12 <- glm(prefmed ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                    + natquota + maj, data = dat, family = binomial)



mediate13 <- gmediate(data = dat, model.m1 = list(model.m11.12, model.m12.12), model.m2 = list(model.m21.12), model.y = model.y2.12, expos = c("sex", 1), cluster = "id_rep")

mediate13$indiv.path
mediate13$second.path

summary(model.m11.12)
summary(model.m12.12)
summary(model.m21.12)
summary(model.y2.12)

confint(model.y2.12)
confint(model.m11.12)
confint(model.m12.12)
confint(model.m21.12)


#APPENDIX FIGURE A4: LEGISLATOR MANDATE CLUSTERED SEs
dat <- read.dta13("LSQ_replication.dta")
dat <- na.omit(dat)

dat$legmand <- paste(dat$id_rep, dat$congress, sep="_")

model.y2.7 <- glm(approv ~ sex + prefmed + comDipPresi + comm_assign + ncomdip + mbloq + nComDipMember + tenure + lnfirm + copartisan_in_executive + divgov 
                  + natquota + maj + totalbills, data = dat, family = binomial)

model.m11.7 <- glm(comDipPresi ~ sex + nComDipMember + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)

model.m12.7 <- glm(comm_assign ~ sex + nComDipMember + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)

model.m21.7 <- glm(prefmed ~ sex + comDipPresi + comm_assign + nComDipMember + copartisan_in_executive + divgov 
                   + natquota + maj, data = dat, family = binomial)



mediate8 <- gmediate(data = dat, model.m1 = list(model.m11.7, model.m12.7), model.m2 = list(model.m21.7), model.y = model.y2.7, expos = c("sex", 1), cluster = "legmand")

mediate8$indiv.path
mediate8$second.path

summary(model.m11.7)
summary(model.m12.7)
summary(model.m21.7)
summary(model.y2.7)

confint(model.y2.7)
confint(model.m11.7)
confint(model.m12.7)
confint(model.m21.7)



